package com.bw.task.customview;

import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.PointF;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.core.content.ContextCompat;

import com.bw.task.R;

import java.util.Collections;

public class MyView extends RelativeLayout {
    Drawable[] drawables = new Drawable[4];
    private  int width;
    private int hight;
    private ImageView imageView;
    public MyView(Context context) {
        super(context);
        init();
    }

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    public void init(){
        drawables[0]= ContextCompat.getDrawable(getContext(), R.drawable.detail_icon_like_grey);
        drawables[1]= ContextCompat.getDrawable(getContext(), R.drawable.detail_icon_like_grey);
        drawables[2]= ContextCompat.getDrawable(getContext(), R.drawable.detail_icon_like_grey);
        drawables[3]= ContextCompat.getDrawable(getContext(), R.drawable.detail_icon_like_grey);
    }
    public void addimgView(){
        imageView = new ImageView(getContext());
        imageView.setImageDrawable(drawables[(int)(Math.random()*drawables.length)]);
        addView(imageView);
        startBaseAnimator(imageView).start();
    }
    public ValueAnimator startBaseAnimator(ImageView imageView){
        MyEvaLuator myEvaLuator = new MyEvaLuator(getPointf(), getPointf());
        ValueAnimator valueAnimator = ValueAnimator.ofObject(myEvaLuator, new PointF(width /5, hight), new PointF((float) (Math.random() * width), (float) (Math.random() * 50)));
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                PointF pointF= (PointF) animation.getAnimatedValue();
                imageView.setX(pointF.x);
                imageView.setY(pointF.y);
                imageView.setAlpha(1-animation.getAnimatedFraction());
            }
        });

        valueAnimator.setTarget(imageView);
        valueAnimator.setDuration(2000);
        return valueAnimator;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width=getMeasuredWidth();
        hight=getMeasuredHeight();
    }

    public PointF getPointf(){
        PointF pointF = new PointF();
        pointF.x = (float) (Math.random()*width/3);
        pointF.y = (float) (Math.random()*hight/3);
        return pointF;
    }

    //4.计算剩余的百分比 3阶贝塞尔曲线
    class MyEvaLuator implements TypeEvaluator<PointF> {
        public MyEvaLuator(PointF pointF1, PointF pointF2) {
            this.pointF1 = pointF1;
            this.pointF2 = pointF2;
        }
        private PointF pointF1;
        private PointF pointF2;
        @Override
        public PointF evaluate(float fraction, PointF startValue, PointF endValue) {
            float time = 1-fraction;
            PointF pointF = new PointF();
            pointF.x=time * time * time * (startValue.x)
                    + 3 * time * time * fraction * (pointF1.x)
                    + 3 * time * fraction * fraction *(pointF2.x)
                    + fraction * fraction * fraction * (endValue.x);
            pointF.y=time * time * time * (startValue.y)
                    + 3 * time * time * fraction * (pointF1.y)
                    + 3 * time * fraction * fraction *(pointF2.y)
                    + fraction * fraction * fraction * (endValue.y);
            return pointF;
        }
    }
}
